<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>个人资料</title>
    <style>
        nav {
            background-color: #f2f2f2;
            padding: 10px 0;
            text-align: center;
        }

        nav a {
            padding: 14px 20px;
            text-decoration: none;
            color: black;
            display: inline-block;
            cursor: pointer; /* 添加鼠标指针样式，让链接看起来可点击 */
        }

        nav a:hover {
            background-color: #ddd;
        }

        .dropdown {
            position: relative;
            display: inline-block;
        }

        .dropbtn {
            padding: 14px 20px;
            text-decoration: none;
            color: black;
            cursor: pointer; /* 添加鼠标指针样式，让链接看起来可点击 */
        }

        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #f9f9f9;
            min-width: 160px;
            box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
            z-index: 1;
        }

        .dropdown:hover .dropdown-content {
            display: block;
        }

        .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
            cursor: pointer; /* 添加鼠标指针样式，让链接看起来可点击 */
        }

        .dropdown-content a:hover {
            background-color: #f1f1f1;
        }
    </style>
    <script>
        function reload(){
            window.location.href="/page/toUserCenterPage";
        }

        function getCookie(cookieName) {
            const strCookie = document.cookie
            const cookieList = strCookie.split(';')

            for(let i = 0; i < cookieList.length; i++) {
                const arr = cookieList[i].split('=')
                if (cookieName === arr[0].trim()) {
                    return arr[1]
                }
            }

            return ''
        }


        document.addEventListener('DOMContentLoaded', function() {
            // 群组点击事件
            document.getElementById('groupLink').addEventListener('click', function(event) {
                if (!checkUserLoggedIn()) {
                    alert('您尚未登录，请先登录');
                    window.location.href = '/page/toLoginPage'; // 模拟跳转到登录页面
                    event.preventDefault();
                }
            });

            // 个人资料点击事件
            document.getElementById('profileLink').addEventListener('click', function(event) {
                if (!checkUserLoggedIn()) {
                    alert('您尚未登录，请先登录');
                    window.location.href = '/page/toLoginPage'; // 模拟跳转到登录页面
                    event.preventDefault();
                }
            });

            // 个人资料点击事件
            document.getElementById('notificationLink').addEventListener('click', function(event) {
                if (!checkUserLoggedIn()) {
                    alert('您尚未登录，请先登录');
                    window.location.href = '/page/toLoginPage'; // 模拟跳转到登录页面
                    event.preventDefault();
                }
            });

            // 校验用户是否已登录的函数
            function checkUserLoggedIn() {
                // 这里可以编写校验cookie中用户是否登录的逻辑，返回 true 或 false
                // 假设这里使用一个名为 isUserLoggedIn 的函数来校验
                return isUserLoggedIn();
            }

            // 这里是模拟的校验用户是否已登录的函数
            function isUserLoggedIn() {
                // 假设在 cookie 中有一个名为 'loggedIn' 的标识，表示用户是否已登录
                const username = getCookie('username')
                // alert(username)
                // 返回 true 表示已登录，返回 false 表示未登录
                return username !== '';

            }
        });
        function addTagInput() {
            var fileCount = document.getElementById("files").files.length;
            var tagInputs = document.getElementById("tagInputs");
            tagInputs.innerHTML = "";
            for (i = 0; i < fileCount; i++) {
                var input = document.createElement("input");
                input.type = "text";
                input.name = "tags";
                input.placeholder = "Enter tags for file " + (i + 1);
                tagInputs.appendChild(input);
            }
        }
        function validateTags() {
            var tags = document.getElementsByName("tags");
            for (var i = 0; i < tags.length; i++) {
                if (tags[i].value.trim() === "") {
                    alert("标签不能为空，请为每个文件添加标签");
                    event.preventDefault(); // 阻止表单提交
                    return;
                }
            }
        }
    </script>
</head>
<body>
<nav>
    <a href="/user/adminhome">首页</a>
    <div class="dropdown">
        <a class="dropbtn">用户管理</a>
        <div class="dropdown-content">
            <a href="/user/searchUser" id="groupLink">用户管理</a>
            <a href="/user/setting" id="chatLink">新增用户</a>
        </div>
    </div>
    <div class="dropdown">
        <a class="dropbtn">个人中心</a>
        <div class="dropdown-content">
            <a href="/page/toAdminCenterPage" id="profileLink">个人资料</a>
            <a href="/page/Exit">登出</a>
        </div>
    </div>
    <a href="/page/toDataPage">数据报表</a>
</nav>
<h1>管理员信息</h1>
<p>用户名: ${userInfo}</p>
<h2>已收藏的文件</h2>
<c:forEach var="fileName" items="${favoriteFiles}">
    <div>
        <span>${fileName}</span>
        <a href="/file/deleteFavorite?fileName=${fileName}" target="_blank"><button>取消收藏</button></a>
    </div>
</c:forEach>
<h2>已上传文件</h2>
<c:forEach var="fileName" items="${uploadedFils}">
    <div>
        <span>${fileName}</span>
        <a href="/file/download?fileName=${fileName}" target="_blank"><button>下载</button></a>
        <a href="/file/preview?fileName=${fileName}" target="_blank"><button>预览</button></a>
        <a href="/file/delete?fileName=${fileName}" target="_blank"><button>删除</button></a>
    </div>
</c:forEach>
<h2>文件上传</h2>
<form action="/file/upload" method="post" enctype="multipart/form-data">
    <label for="files">选择文件:</label>
    <input type="file" name="files" id="files" multiple accept=".pdf" required>
    <div id="tagInputs"></div>
    <button type="button" onclick="addTagInput()">添加标签输入框</button>
    <button type="submit" onclick="validateTags()">一键上传</button>
</form>
</body>
</html>